package com.yangjun.service.departmentCustomization;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.DVConstants;
import com.yangjun.entity.departmentCustomizationAnalysis.PreSalesSectionOverViewEntity;
/**
 * 
 * @ClassName PreSalesPharmacistService
 * @Description 可视化部门定制售前科组单品转化的内容
 * @author Administrator
 * @Date 2017年3月15日 上午10:35:01
 * @version 1.0.0
 */
@Service
public class PreSalesSingleProductService extends DepartmentCustomizationCommonService{
	
	/**
	 * 
	 * @Description 模糊查询单品的下拉列表
	 * @return
	 * @author yangjun
	 * @Date 2017年3月13日 上午9:50:55
	 */
	public List<String> querySingleProduct(){
		return preSalesSectionMapper.querySingleProductList();
	}
	/**
	 * @Description 重点产品的排行
	 * @param groupName
	 * @param wareName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月21日 下午3:47:22
	 */
	//任务完成率排名
	public Map<String,Object> queryRank(String channelName,String sourceName,String roomName){
		Map<String,Object> resultMap = Maps.newHashMap();
		List<PreSalesSectionOverViewEntity> adviceCntList = this.preSalesSectionMapper.querySingleProductRank(channelName,sourceName,roomName,"advice_cnt");
		List<PreSalesSectionOverViewEntity> orderCntList = this.preSalesSectionMapper.querySingleProductRank(sourceName,sourceName,roomName,"order_cnt");
		List<Map<String,Object>> adviceCntSortList= rankPreSalesSectionOverViewEntityList(adviceCntList);
		List<Map<String,Object>> orderCntSortList= rankPreSalesSectionOverViewEntityList(orderCntList);
		sortRankPreSalesSectionOverViewEntityList(adviceCntSortList,new FloatComparatorSortList("bar"));
		sortRankPreSalesSectionOverViewEntityList(orderCntSortList,new FloatComparatorSortList("bar"));

		resultMap.put("adviceCnt",adviceCntSortList);
		resultMap.put("orderCnt",orderCntSortList);
		return resultMap;
	}
	
	public List<Object[]> queryWareTable(String channelName,String sourceName,String roomName,String wareCode,String wareName){
		List<Object[]> wareRankResultList = Lists.newArrayList();
		
		List<PreSalesSectionOverViewEntity> wareRankList = this.preSalesSectionMapper.queryWareRankTable(channelName,sourceName,roomName,wareCode,wareName);

		if(wareRankList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : wareRankList){
				String warecode = preSalesSectionOverViewEntity.getWareCode();
				String warename = preSalesSectionOverViewEntity.getWareName();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount();
				Object[] dataArr = new Object[]{warecode,warename,adviceCnt,orderCnt,salesOrderAmount};
				wareRankResultList.add(dataArr);
			}
		}
	
		return wareRankResultList;
	}
	public List<Object[]> queryChannelTable(String channelName,String sourceName,String roomName,String wareCode,String wareName){
		List<Object[]> channelResultList = Lists.newArrayList();
		List<PreSalesSectionOverViewEntity> channelList = this.preSalesSectionMapper.queryChannelTable(channelName,sourceName,roomName,wareCode,wareName);
		if(channelList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : channelList){
				String warecode = preSalesSectionOverViewEntity.getChannel();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt();
				int validAdviceCnt= preSalesSectionOverViewEntity.getValidAdviceCnt();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount();
				Object[] dataArr = new Object[]{warecode,adviceCnt,validAdviceCnt,orderCnt,salesOrderAmount};
				channelResultList.add(dataArr);
			}
		}
		return channelResultList;
	}

	
	public Map<String,Object> querySingleProductComparisonLineChartByDayofMonth(String channelName,String sourceName,String roomName,String wareCode,String wareName){
		String currentDate = sdfMonth.format(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		String contrastDate = sdfMonth.format(calendar.getTime());
		String[] dateArr = {currentDate,contrastDate};
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> LineChartList = this.preSalesSectionMapper.querySingleProductComparisonLineChartByDay(channelName,sourceName,roomName,wareCode,wareName,dateArr);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		return queryLineChartData(DVConstants.MONTH_DAY,LineChartList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}
	
	public Map<String,Object> querySingleProductComparisonLineChartByWeek(String channelName,String sourceName,String roomName,String wareCode,String wareName){
		Calendar calendar = Calendar.getInstance();
		String currentDate = sdfDay.format(new Date());
		calendar.setTime(new Date());
		int dayOfWeek= calendar.get(Calendar.DAY_OF_WEEK)-1;
		calendar.add(Calendar.DAY_OF_MONTH, -(dayOfWeek+6));
		String contrastDate = sdfDay.format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, +6);
		Date tmpDate = calendar.getTime();
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> LineChartList = setDayOfWeek(preSalesSectionMapper.querySingleProductComparisonLineChartByWeek(channelName,sourceName,roomName,wareCode,wareName,currentDate,contrastDate),tmpDate);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
		return queryLineChartData(DVConstants.WEEK_NAME_ARR,LineChartList,new String[]{"本周","上周"},DVConstants.COMPARISON_LEGEND,index);
	}
	public Map<String,List<String[]>> queryKeyProductTableDataByMonth(String channelName,String sourceName,String roomName,String wareCode,String wareName){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询12个月的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.querySingleProductTableData(channelName,sourceName,roomName,wareCode,wareName,beginDate,endDate,"MONTH");
		return queryTableData(tableByDayList,DVConstants.MONTH_DAY,"channel","month");
	}
	
}
